继续Part1部分–Activity to Activity,这个部分我们简单介绍下Fragment to Fragment的共享动画实现;
先看效果
Fragment to Fragment
首先我们需要创建一个Activity容器来加载Fragment:
按照代码所示,我们现在Activity中加载FragmentA 然后由FragmentA跳转到FragmentB,并且实现共享动画。
下面实现FragmentA:
FragmentA中的xml代码实现,一个ImageView 和一个Button ,其中ImageView为FragmentA的共享元素,并且为他设置属性android:transitionName="simple transition name"
:
|
|
FragmentA的java代码
|
|
值得注意的是在addShareElement()
这个方法以及addToBackStack()
这个方法;
- addShareElement:设置了作为共享元素的控件以及transitionName;
- addToBackStack:为了让fragment回栈,如果不设置这个回栈,当跳转到fragmentB的时候,BackClick就会直接退出Activity;
那么在FragmentB就比较容易实现了:
FragmentB:
|
|
只需要在FragmentB的
- xml 的共享控件里面设置
android:transitionName="simple transition name"
- onCreate里面设置动画为android.R.transition.move
加载网络图片的元素共享(以Picasso为例)
一般在App中,我们的ImageView都是在网络URL获取的资源,那么网络加载的ImageView也是可以实现共享元素转换的,下面我们就以Picasso为例:
添加依赖
|
|
FragmentA中
|
|
FragmentB中添加加载图片的:
|
|
- 添加noFade禁用渐隐的效果促使动画更加的流畅。
- 在CallBack的onSuccess()中设置
startPostponedEnterTransition()
。
非常感谢,你能耐心读完;